layui.config({
    base: '/static/layui/'
}).extend({
    tableSelect: 'tableSelect'
}).use(['form', 'layer', 'jquery', 'table', 'laydate', 'upload','tableSelect'], function () {
    const form = layui.form;
    const table = layui.table;
    const tableSelect = layui.tableSelect;
    const $ = layui.jquery;

    var bomId;

    form.render()

    table.render({
        elem: '#table'
        ,title: '列表'
        ,method: 'post'
        ,height: 'full-100'
        ,page: true //开启分页
        ,limit: 20
        ,limits: [20, 30, 40, 50, 60, 70, 80, 90, 100]
        ,loading: true
        ,cols: [
            [
                { field: 'bmb01', title: '主件料号(⼚商料号) '},
                { field: 'ima021m', title: '主件料号的规格栏位(纬创料号) ' },
                { field: 'ima02m', title: '主件料号的品名或料号描述' },
                { field: 'bmb02', title: '单身项次(内部版本管控)' },
                { field: 'bmb03', title: '下阶料号(⼚商料号) ' },
                { field: 'ima021', title: '下阶料号规格栏位(纬创料号)' },
                { field: 'ima02', title: '主件料号的品名或料号描述' },
                { field: 'qpa', title: '下阶料号QPA(单位⽤量 ps/g)' },
                { field: 'materialType', title: '材料类型' },
            ]
        ]
        ,text: {
            none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
        }
        ,data: []
        ,toolbar: '#tableToolbar'
        ,defaultToolbar: ['filter']
    });

    tableSelect.render({
        elem: '#cusCode',
        checkedKey: 'cusCode',
        searchPlaceholder: '企业编码',
        searchKey: 'cusCode',
        table: {
            url: 'manage/common/list/customer'
            ,method: 'post'
            ,cols: [
                [
                    { type: 'radio' },
                    { field: 'cusCode', title: '企业编码' },
                    { field: 'cusName', title: '企业名称' },
                ]
            ]
        },
        done: function (elem, data) {
            var NEWJSON = []
            layui.each(data.data, function (index, item) {
                NEWJSON.push(item.cusCode)
            })
            elem.val(NEWJSON.join(","))

            tableSelect.render({
                elem: '#material',
                checkedKey: 'invCode',
                searchPlaceholder: '料号编码',
                searchKey: 'invCode',
                table: {
                    url: 'manage/select/getMaterial',
                    where: {cusCode:NEWJSON.join(",")},
                    cols: [
                        [
                            { type: 'radio' },
                            { field: 'invCode',width: 150, title: '料号编码' },
                            { field: 'invName',width: 150, title: '料号名称' },
                            { field: 'cusCode',width: 100, title: '企业编码' },
                            { field: 'cusName',width: 150, title: '企业名称' },
                        ]
                    ]
                },
                done: function (elem, data) {
                    var NEWJSON = []
                    layui.each(data.data, function (index, item) {
                        NEWJSON.push(item.invCode)
                    })
                    elem.val(NEWJSON.join(","))
                }
            })
        }
    })


    table.on('toolbar(table)', function(obj){
        switch (obj.event) {
            case 'download':
                let cusCode = $('#cusCode').val();
                if(!cusCode){
                    layer.msg("请选择供应商", {icon: 2});
                    return;
                }
                layer.confirm('确定要导出数据并下载到本地吗？', function (index) {
                    window.location.href = "/base_material/getReportDow?str=前&cusCode=" + cusCode;
                    layer.close(index);
                });
                break;
            case 'find':
                if($('#cusCode').val() == ''){
                    layer.msg("请选择供应商编码", {icon: 2});
                    return;
                }
                let obj = {
                    cusCode:$('#cusCode').val(),
                    invCode:$('#material').val()
                    // invDef:$('#cInvDefine9').val(),
                }
                $.post('base_material/iosBaseList',obj,function (d){
                    table.reload('table', {
                        data: d.data
                        ,page: {
                            curr: 1
                        }
                    });
                })
                break;
        }
    })

})